<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
        <div class="center_box">
            <div class="clearfix">
            	<div class="consult_box bg_white">
                	<div class="consult_list">
                        <div class="consult_inner" id="talkContent">
                            <span class="consult_notice">婴贝轩为您免费提供最专业的咨询服务！<b class="consult_bt"></b></span>
                        </div>    
                        <div id="msg_end" style="height:0px; overflow:hidden"></div>                
                    </div>                    
                    <div class="consult_textar">
                    	<textarea name="content" id="talkForm_content_tmp" onkeyup="changeSizeInfo();" maxlength="100"></textarea>                        
                    </div>	                    
                    <div class="ct_detail_expr" id="detailFace">
                        <!-- class="ct_detail_face_box"-->
<!--                         <span> -->
<!--                             <a class="float_lf ct_detail_face"></a> -->
<!--                          </span>                        -->
                    </div>
                    
                    <a class="float_rg ct_detail_cm" href="javascript:sentTalk();">发&nbsp;&nbsp;送</a>
                    <span class="float_rg consult_word">您还可以输入<b id="showSize">100</b>个字 </span>
                    
                </div>
              	<div class="context_rank_box consult_right" id="talk_baby_info" style="display:none;">            
                    <div class="context_inner clearfix">
                    	<h2>宝宝信息</h2>
                        <div class="context_titles">
                        	<p><img id="s_babyPic" width="180" height="180" /></p>
                            <p>昵称：<span id="s_babyName"></span></p>
                            <p>性别：<span id="s_babySex"></span></p>
                            <p>生日：<span id="s_babyBirthday"></span></p>
                            <p>身高：<span id="s_babyHeight"></span> 厘米</p>
                            <p>体重：<span id="s_babyWeight"></span> 千克</p>
                        </div>
                    </div>
                </div>
                <form id="talkForm">
                    <input type="hidden" name="getAccount" value="<%=request.getParameter("getAccount")%>"/>
                    <input type="hidden" name="content" id="talkForm_content"/>
                </form>
            </div>	
        </div>
        
<script type="text/javascript">
<!--
var talkData=new Array();
//查询商品列表
function doTalkQuery(){	
	var url="${ctx}/frontTalk!getList.action";
	$.post(url, 'getAccount=<%=request.getParameter("getAccount")%>' ,function callback(txt){ 
			var json = $.parseJSON(txt);
			if (json.code==0) {	
				createNotes(json.data)
			}else{
				alert('查询失败！');
			}
		}	
	);
}
//查找是否已经在显示的数组中
function isInTalks(obj){
	var flag=false;
	for(var i=0;i<talkData.length;i++){
		if(talkData[i].id==obj.id){
			flag=true;
			break;
		}
	}
	return flag;
}

//创建列表数据
function createNotes(objs){		
	var c='';
	for(var i=0;i<objs.length;i++){
		var obj=objs[i];
		if(isInTalks(obj)){
			continue;
		}
		var q=obj.request?"consult_answer":"consult_question";
		c+='<div class="'+q+'">';
		c+='<p class="consult_per">'+obj.talkAccountName+'  '+formatDate(obj.createTime)+'</p>';
		c+='<p class="consult_context">'+obj.content+'</p>';
		c+='</div>';	
		talkData.push(obj);
	}	
	$('#talkContent').append(c);
	msg_end.scrollIntoView(); 
}
//格式化日期：yyyy-MM-dd HH:mm:ss
function formatDate(time){
	var date=new Date(time);
	var m = date.getMonth()+1;
	var d = date.getDate();
	var H=date.getHours();
	var n=date.getMinutes();
	var s=date.getSeconds();
	return (m<10?('0'+m):m)+"-"+(d<10?('0'+d):d)+" "+(H<10?('0'+H):H)+":"+(n<10?('0'+n):n)+":"+(s<10?('0'+s):s);
}
//格式化日期：yyyy-MM-dd
function formatShotDate(time){
	var date=new Date(time);
	var m = date.getMonth()+1;
	var d = date.getDate();
	return date.getFullYear()+"-"+(m<10?('0'+m):m)+"-"+(d<10?('0'+d):d);
}
//发送对话
function sentTalk(){	
	if($('#talkForm_content_tmp').val()=='')return;
	$('#talkForm_content').val($('#talkForm_content_tmp').val());
	$('#talkForm_content_tmp').val('');
	changeSizeInfo();
	//
	$.post("${ctx}/frontTalk!add.action", $("#talkForm").serialize() ,function callback(txt){   
			var json = $.parseJSON(txt);
			if (json.code==0) {	
				doTalkQuery();			
			}else{
				alert('发送对话失败！');
			}
		}	
	);
}
//改变数字
function changeSizeInfo(){
	$('#showSize').html(100-$('#talkForm_content_tmp').val().length);
}

//对话栏右边宝宝信息
function talkBabyInfo(){
	var url="${ctx}/frontSys!getCurrentUser.action";
	$.post(url, "" ,function callback(txt){   
			var json = $.parseJSON(txt);
			if (json.code==0) {	
				if(json.info==null){
					$('#talk_baby_info').hide();
				}else {
                    $('#s_babyName').html(json.info.babyName);
                    $('#s_babySex').html(json.info.babySex==1?'男':'女');
                    $('#s_babyBirthday').html(formatShotDate(json.info.babyBirthday));
                    $('#s_babyHeight').html(json.info.babyHeight);
                    $('#s_babyWeight').html(json.info.babyWeight);
                    $('#s_babyPic').attr('src','${ctx}/frontSys!getPic.action?name='+json.info.babyPic);
                    
					$('#talk_baby_info').show();					
				}
			}
		}	
	);
}

//对话栏右边宝宝信息
function talkBabyInfo2(){
	var url="${ctx}/talk!getUserByAccount.action";
	$.post(url, "account=<%=request.getParameter("getAccount")%>" ,function callback(txt){   
			var json = $.parseJSON(txt);
			if (json.code==0) {	
				if(json.info==null){
					$('#talk_baby_info').hide();
				}else {
                    $('#s_babyName').html(json.info.babyName);
                    $('#s_babySex').html(json.info.babySex==1?'男':'女');
                    $('#s_babyBirthday').html(formatShotDate(json.info.babyBirthday));
                    $('#s_babyHeight').html(json.info.babyHeight);
                    $('#s_babyWeight').html(json.info.babyWeight);
                    $('#s_babyPic').attr('src','${ctx}/frontSys!getPic.action?name='+json.info.babyPic);
					$('#talk_baby_info').show();					
				}
			}
		}	
	);
}
//响应回车发表
$('#talkForm_content_tmp').bind('keyup', function (e) {
    var key = e.which;
    if (key == 13) {
    	sentTalk();
    }
});

doTalkQuery();
setInterval('doTalkQuery()',4000);
//-->
</script>